<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 6.3.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/personalBlog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/personalBlog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/personalBlog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/personalBlog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/personalBlog/css/main.css">


<link rel="stylesheet" href="/personalBlog/lib/font-awesome/css/all.min.css">

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"tobylinn.github.io","root":"/personalBlog/","scheme":"Muse","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":false,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="DOM Document Object Model (DOM) 中文名为文档对象模型,它是一个编程的接口(API),允许我们在文档创建、修改、删除、查找元素;我们还可以获取元素并绑定事件,使页面更加的动态;   DOM 将 HTML 文档视为节点树,每一个节点代表一个 html 元素;DOM 节点分为元素节点、文本节点、属性节点">
<meta property="og:type" content="article">
<meta property="og:title" content="DOM">
<meta property="og:url" content="https://tobylinn.github.io/personalBlog/4150170345/index.html">
<meta property="og:site_name" content="Toby&#39;s Blog">
<meta property="og:description" content="DOM Document Object Model (DOM) 中文名为文档对象模型,它是一个编程的接口(API),允许我们在文档创建、修改、删除、查找元素;我们还可以获取元素并绑定事件,使页面更加的动态;   DOM 将 HTML 文档视为节点树,每一个节点代表一个 html 元素;DOM 节点分为元素节点、文本节点、属性节点">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://tobylinn.github.io/personalBlog/img/image.png">
<meta property="article:published_time" content="2023-10-27T15:14:41.000Z">
<meta property="article:modified_time" content="2023-10-27T16:51:44.675Z">
<meta property="article:author" content="Toby Lin">
<meta property="article:tag" content="前端">
<meta property="article:tag" content="DOM">
<meta property="article:tag" content="JavaScript">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://tobylinn.github.io/personalBlog/img/image.png">

<link rel="canonical" href="https://tobylinn.github.io/personalBlog/4150170345/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>DOM | Toby's Blog</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/personalBlog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">Toby's Blog</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/personalBlog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/personalBlog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
  </ul>
</nav>




</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://tobylinn.github.io/personalBlog/4150170345/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/personalBlog/images/avatar.gif">
      <meta itemprop="name" content="Toby Lin">
      <meta itemprop="description" content="無限進步!">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Toby's Blog">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          DOM
        </h1>

        <div class="post-meta">
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>

              <time title="创建时间：2023-10-27 23:14:41" itemprop="dateCreated datePublished" datetime="2023-10-27T23:14:41+08:00">2023-10-27</time>
            </span>
              <span class="post-meta-item">
                <span class="post-meta-item-icon">
                  <i class="far fa-calendar-check"></i>
                </span>
                <span class="post-meta-item-text">更新于</span>
                <time title="修改时间：2023-10-28 00:51:44" itemprop="dateModified" datetime="2023-10-28T00:51:44+08:00">2023-10-28</time>
              </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/personalBlog/categories/JavaScript/" itemprop="url" rel="index"><span itemprop="name">JavaScript</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/personalBlog/categories/JavaScript/3-DOM/" itemprop="url" rel="index"><span itemprop="name">3.DOM</span></a>
                </span>
            </span>

          <br>
            <span class="post-meta-item" title="本文字数">
              <span class="post-meta-item-icon">
                <i class="far fa-file-word"></i>
              </span>
                <span class="post-meta-item-text">本文字数：</span>
              <span>3.8k</span>
            </span>
            <span class="post-meta-item" title="阅读时长">
              <span class="post-meta-item-icon">
                <i class="far fa-clock"></i>
              </span>
                <span class="post-meta-item-text">阅读时长 &asymp;</span>
              <span>6 分钟</span>
            </span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <h2 id="DOM"><a href="#DOM" class="headerlink" title="DOM"></a>DOM</h2><blockquote>
<p>Document Object Model (DOM) 中文名为文档对象模型,它是一个编程的接口(API),允许我们在文档创建、修改、删除、查找元素;我们还可以获取元素并绑定事件,使页面更加的动态;</p>
</blockquote>
<blockquote>
<p>DOM 将 HTML 文档视为节点树,每一个节点代表一个 html 元素;DOM 节点分为元素节点、文本节点、属性节点</p>
</blockquote>
<span id="more"></span>

<h3 id="查找节点"><a href="#查找节点" class="headerlink" title="查找节点"></a>查找节点</h3><ul>
<li><p>getElement 系列</p>
<blockquote>
<p>返回一个或多个节点集合, HTMLCollection 对象(动态 nodeList 对象)</p>
</blockquote>
</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">1. getElementById</span><br><span class="line">根据节点 id 属性去查找元素(出现多个相同 id ,则返回查找第一个的元素节点)</span><br><span class="line"></span><br><span class="line">2. getElementsByClassName</span><br><span class="line">根据节点 class 属性去查找元素,返回节点集合(可以出现多个 html 节点)</span><br><span class="line"></span><br><span class="line">3.getElementsByTagName</span><br><span class="line">根据标签名去查找元素,返回节点集合</span><br><span class="line"></span><br><span class="line">4.getElementsByName</span><br><span class="line">根据标签属性名去查找元素,返回 nodeList 集合(可以使用 forEach)</span><br></pre></td></tr></table></figure>

<ul>
<li><p>querySelector 系列</p>
<blockquote>
<p>返回一个或多个节点集合, nodeList 对象(静态 nodeList 对象),支持 css3 选择器</p>
</blockquote>
</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">1.querySelector(选择器)</span><br><span class="line">返回匹配的第一个元素</span><br><span class="line"></span><br><span class="line">2.querySelectorAll(选择器)</span><br><span class="line">返回匹配的所有元素(节点集合,类数组)</span><br></pre></td></tr></table></figure>

<ul>
<li>查找父节点</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">parentNode 返回当前元素的父节点(文本/元素)</span><br><span class="line"></span><br><span class="line">parentElement 返回当前元素的父元素节点</span><br></pre></td></tr></table></figure>

<ul>
<li><p>previousElementSibling 查找前一个同级(兄弟)元素</p>
</li>
<li><p>previousSibling 查找前一个同级(兄弟)节点(包含文本节点&#x2F;元素节点)</p>
</li>
<li><p>nextElementSibling 查找后一个同级(兄弟)元素</p>
</li>
<li><p>nextSibling 查找后一个同级(兄弟)节点(包含文本节点&#x2F;元素节点)</p>
</li>
<li><p>查找子节点</p>
</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">childNodes 返回包含文本节点的集合( nodeList 对象)</span><br><span class="line"></span><br><span class="line">children 返回元素节点集合( HTMLCollection 对象)</span><br><span class="line"></span><br><span class="line">firstChild 返回第一个子节点(包含文本节点)</span><br><span class="line"></span><br><span class="line">firstElementChild 返回第一个子元素</span><br><span class="line"></span><br><span class="line">lastChild 返回最后一个子节点(包含文本节点)</span><br><span class="line"></span><br><span class="line">lastElementChild 返回最后一个子元素</span><br></pre></td></tr></table></figure>

<h3 id="操作类名-动态切换类名"><a href="#操作类名-动态切换类名" class="headerlink" title="操作类名(动态切换类名)"></a>操作类名(动态切换类名)</h3><ul>
<li><p>className</p>
<blockquote>
<p>可读取或重置元素 class 属性</p>
</blockquote>
</li>
<li><p>classList</p>
<blockquote>
<p>返回元素类名列表(DOMTokenList 对象),classList 的属性只是可读,如果要操作这个对象,必须使用 classList 的内置方法</p>
</blockquote>
</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><span class="line">node.classList.add(&#x27;xx&#x27;,&#x27;xx&#x27;,&#x27;xx&#x27;,...)</span><br><span class="line">在元素中添加一个或多个类名</span><br><span class="line"></span><br><span class="line">node.classList.remove(&#x27;xxxx&#x27;)</span><br><span class="line">在元素中添加一个或多个类名</span><br><span class="line"></span><br><span class="line">node.classList.toggle(切换的类名,true || false)</span><br><span class="line">在元素中切换一个或多个类名</span><br><span class="line">true 只有不存在类名的时候才会添加</span><br><span class="line">false 只有存在类名的时候才会移除</span><br><span class="line">不填布尔值: 强制添加类名和强制移除类名</span><br><span class="line"></span><br><span class="line">node.classList.contains(类名)</span><br><span class="line">返回布尔值,指定类名是否存在</span><br></pre></td></tr></table></figure>

<h3 id="属性节点"><a href="#属性节点" class="headerlink" title="属性节点"></a>属性节点</h3><ul>
<li><p>setAttribute(属性名,属性值) 设置元素属性(返回字符串)</p>
</li>
<li><p>getAttribute(属性名) 获取元素属性(返回数字)</p>
</li>
<li><p>removeAttribute(属性名) 删除元素属性</p>
</li>
</ul>
<h3 id="添加节点"><a href="#添加节点" class="headerlink" title="添加节点"></a>添加节点</h3><ul>
<li><p>createElement(元素名称) 创建元素节点</p>
</li>
<li><p>appendChild(元素)</p>
</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">功能:</span><br><span class="line">  将一个节点添加到指定父元素的末尾处.如果被添加的节点已经存在,会将原来存在的元素位置移动到新的位置上</span><br></pre></td></tr></table></figure>

<ul>
<li>append(元素)</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">功能:</span><br><span class="line">  将一个节点添加到指定父元素的末尾处(可以添加字符串/数字等)</span><br></pre></td></tr></table></figure>

<ul>
<li><p>createTextNode(文本内容) 创建文本节点</p>
</li>
<li><p>insertBefore(插入的新节点, 指定的元素之前插入)</p>
</li>
<li><p>replaceChild(替换新节点, 需要被替换的旧节点)</p>
</li>
<li><p>cloneNode(布尔值) 默认值:flase</p>
</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">功能:</span><br><span class="line">  克隆节点,参数接收一个布尔值, true 深克隆(当前元素的所有层级都克隆), false 只会克隆当前元素(默认值)</span><br></pre></td></tr></table></figure>

<h3 id="删除节点"><a href="#删除节点" class="headerlink" title="删除节点"></a>删除节点</h3><ul>
<li><p>remove() 删除自身</p>
</li>
<li><p>removeChild(子节点) 删除子节点</p>
</li>
</ul>
<h3 id="补充"><a href="#补充" class="headerlink" title="补充"></a>补充</h3><ul>
<li><p>innerHTML 设置或获取位于元素开始和结束标签内的 html</p>
</li>
<li><p>outerHTML 设置或获取位于元素以及元素开始和结束标签内容的 <code>html</code> 形式(当前元素+元素内容)</p>
</li>
<li><p>innerText 设置或获取位于元素开始和结束标签内的<code>文本</code>(元素文本内容,不包含元素&#x2F;注释节点)</p>
</li>
<li><p>outerText 设置或获取位于元素以及元素开始和结束标签内容的 <code>文本</code> 形式 (当前元素 + 元素内文本内容, 不包含元素 &#x2F; 注释节点)</p>
</li>
</ul>
<p><img src="/personalBlog/./img/image.png" alt="Alt text"></p>
<h2 id="事件"><a href="#事件" class="headerlink" title="事件"></a>事件</h2><blockquote>
<p>事件是 js 和 DOM 的交互桥梁(对事件做出反应),事件通常是和函数结合使用,通过用户操作行为(点击&#x2F;手指触摸&#x2F;鼠标)事件来驱动函数的执行(回调函数)</p>
</blockquote>
<h3 id="js-事件等级"><a href="#js-事件等级" class="headerlink" title="js 事件等级"></a>js 事件等级</h3><ul>
<li>DOM 0 级事件</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line">优点:具有极好的跨浏览器优势,会以最快的速度去绑定事件</span><br><span class="line"></span><br><span class="line">写法:</span><br><span class="line">元素.on事件名 = 函数</span><br><span class="line"></span><br><span class="line">绑定方式:</span><br><span class="line">1. html 标签内绑定</span><br><span class="line">&lt;div onclick = &#x27;函数&#x27;&gt;&lt;/div&gt;</span><br><span class="line"></span><br><span class="line">2. 在 script 标签内绑定</span><br><span class="line">div.onclick = function()&#123;&#125;</span><br></pre></td></tr></table></figure>

<ul>
<li>DOM 1 级事件</li>
</ul>
<blockquote>
<p>1 级 DOM 标准(无提出相关的事件内容)</p>
</blockquote>
<ul>
<li>DOM 2 级事件</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">特点:</span><br><span class="line">addEventListener 使用除了 IE 以外的浏览器, IE 使用 attachEvent 绑定事件;同个元素在同一个事件上可以用不同的处理函数</span><br><span class="line"></span><br><span class="line">写法:</span><br><span class="line">元素.addEventListener(事件类型,函数,事件流执行阶段)</span><br><span class="line"></span><br><span class="line">addEventListener 名为事件监听器,主要功能往事件目标注册一个事件监听器,接收回调执行函数</span><br><span class="line"></span><br><span class="line">事件流执行阶段 接收一个布尔值,默认是 false ,表示冒泡阶段触发; true 表示捕获阶段触发</span><br></pre></td></tr></table></figure>

<ul>
<li>DOM 3 级事件</li>
</ul>
<blockquote>
<p>推出更多的事件类型,例如:blur、focus、keydown、keyup、dblclick、scroll、load、mouseenter、mouseout、touch、drag、change、input</p>
</blockquote>
<h3 id="事件流"><a href="#事件流" class="headerlink" title="事件流"></a>事件流</h3><blockquote>
<p>事件流表述的页面接收事件的触发顺序,页面触发一个事件,会按照一定的顺序来响应事件,事件的响应过程就是事件流(过程)</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">事件流三大阶段</span><br><span class="line">1. 冒泡阶段 (IE提出)</span><br><span class="line">事件冒泡,事件开始时由最具体的元素接收,然后逐级向上传播</span><br><span class="line"></span><br><span class="line">2. 目标阶段</span><br><span class="line">触发事件的对象( event target )</span><br><span class="line"></span><br><span class="line">3. 捕获阶段 (网景提出)</span><br><span class="line">事件捕获,事件开始时由最具体的元素接收,从最外层html元素往里捕获</span><br></pre></td></tr></table></figure>

<h4 id="阻止冒泡-传播"><a href="#阻止冒泡-传播" class="headerlink" title="阻止冒泡(传播)"></a>阻止冒泡(传播)</h4><ul>
<li><p>event.stopPropagation()</p>
</li>
<li><p>window.event.cancelBubble &#x3D; true (IE 取消泡泡)</p>
</li>
</ul>
<h4 id="组织默认行为"><a href="#组织默认行为" class="headerlink" title="组织默认行为"></a>组织默认行为</h4><blockquote>
<p>例如:表单提交、a 链接跳转等，如果想要控制，必须要阻止默认行为(自带行为)</p>
</blockquote>
<ul>
<li><p>event.preventDefault()</p>
</li>
<li><p>window.event.returnValue &#x3D; false (IE)</p>
</li>
</ul>
<h3 id="事件代理-事件委托"><a href="#事件代理-事件委托" class="headerlink" title="事件代理(事件委托)"></a>事件代理(事件委托)</h3><blockquote>
<p>事件代理的实现原理就是利用了冒泡阶段和目标阶段,把处理的事件添加到父元素上,等待子元素的事件冒泡,并且可以要通过父元素 event.target 判断具体的子元素,从而做出代理</p>
</blockquote>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line">优点:</span><br><span class="line">1. 大量节约内存的占用,减少事件的注册</span><br><span class="line">2. 可以实现新增子节点的时候,无需再次注册事件,对动态内容操作尤为合适</span><br><span class="line"></span><br><span class="line">缺点:</span><br><span class="line">1. 会自动触发事件冒泡</span><br><span class="line">2. 缺失下标,需要手动添加下标自定义属性</span><br></pre></td></tr></table></figure>

<h3 id="拖拽事件-drag"><a href="#拖拽事件-drag" class="headerlink" title="拖拽事件 drag"></a>拖拽事件 drag</h3><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line">1. dragstart 拖动开始时触发</span><br><span class="line"></span><br><span class="line">2. drag 拖动时触发</span><br><span class="line"></span><br><span class="line">3. dragend 拖动结束时触发</span><br><span class="line"></span><br><span class="line">4. dragover 进入目标区域就会触发（是否进入区域取决于鼠标位置）</span><br><span class="line"></span><br><span class="line">5. dragleave 拖动元素没有放下就离开目的区域的时候触发（是否进入区域取决于鼠标位置）</span><br><span class="line"></span><br><span class="line">6. dragenter 进入目标区域所占屏幕空间触发（是否进入区域取决于鼠标位置）</span><br><span class="line"></span><br><span class="line">7. drop 选择文本或内容放置到有效的目标内触发（是否进入区域取决于鼠标位置）</span><br><span class="line"></span><br><span class="line">`dragover` 和 `dragenter` 默认行为是不接受放置拖动元素，必须要阻止事件的默认行为 (浏览器不允许)</span><br><span class="line"></span><br><span class="line">拖动元素到达目标元素的顺序</span><br><span class="line">dragstart - drag - dragenter -dragover - dragleave - dragend</span><br></pre></td></tr></table></figure>

<h3 id="移动端-touch"><a href="#移动端-touch" class="headerlink" title="移动端 touch"></a>移动端 touch</h3><h4 id="移动事件的类型"><a href="#移动事件的类型" class="headerlink" title="移动事件的类型"></a>移动事件的类型</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">1. touchstart 开始触摸屏幕</span><br><span class="line">2. touchmove 触摸屏幕开始拖动手指(一直触发)</span><br><span class="line">3. touchend 触摸屏幕后移开屏幕</span><br><span class="line">4. touchcancel 触摸被取消(系统取消),例如电话打断/关机(游戏存档/草稿能力/表单保存提示)</span><br></pre></td></tr></table></figure>

<h4 id="touch-事件对象属性"><a href="#touch-事件对象属性" class="headerlink" title="touch 事件对象属性"></a>touch 事件对象属性</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">1. touches 屏幕上所有的触摸点</span><br><span class="line">2. pageX pageY 相对页面的左上角X轴/Y轴坐标</span><br><span class="line">3. clientX clientY 相对可视窗口的左上角X轴/Y轴坐标</span><br><span class="line">4. screenX screenY 相对屏幕的左上角X轴/Y轴坐标</span><br><span class="line">5. target 触发事件的目标元素</span><br></pre></td></tr></table></figure>

    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/personalBlog/tags/%E5%89%8D%E7%AB%AF/" rel="tag"># 前端</a>
              <a href="/personalBlog/tags/DOM/" rel="tag"># DOM</a>
              <a href="/personalBlog/tags/JavaScript/" rel="tag"># JavaScript</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/personalBlog/4090752603/" rel="prev" title="BOM">
      <i class="fa fa-chevron-left"></i> BOM
    </a></div>
      <div class="post-nav-item"></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#DOM"><span class="nav-number">1.</span> <span class="nav-text">DOM</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%9F%A5%E6%89%BE%E8%8A%82%E7%82%B9"><span class="nav-number">1.1.</span> <span class="nav-text">查找节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%93%8D%E4%BD%9C%E7%B1%BB%E5%90%8D-%E5%8A%A8%E6%80%81%E5%88%87%E6%8D%A2%E7%B1%BB%E5%90%8D"><span class="nav-number">1.2.</span> <span class="nav-text">操作类名(动态切换类名)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%B1%9E%E6%80%A7%E8%8A%82%E7%82%B9"><span class="nav-number">1.3.</span> <span class="nav-text">属性节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%B7%BB%E5%8A%A0%E8%8A%82%E7%82%B9"><span class="nav-number">1.4.</span> <span class="nav-text">添加节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%88%A0%E9%99%A4%E8%8A%82%E7%82%B9"><span class="nav-number">1.5.</span> <span class="nav-text">删除节点</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%A1%A5%E5%85%85"><span class="nav-number">1.6.</span> <span class="nav-text">补充</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8B%E4%BB%B6"><span class="nav-number">2.</span> <span class="nav-text">事件</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#js-%E4%BA%8B%E4%BB%B6%E7%AD%89%E7%BA%A7"><span class="nav-number">2.1.</span> <span class="nav-text">js 事件等级</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%8B%E4%BB%B6%E6%B5%81"><span class="nav-number">2.2.</span> <span class="nav-text">事件流</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%98%BB%E6%AD%A2%E5%86%92%E6%B3%A1-%E4%BC%A0%E6%92%AD"><span class="nav-number">2.2.1.</span> <span class="nav-text">阻止冒泡(传播)</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%BB%84%E7%BB%87%E9%BB%98%E8%AE%A4%E8%A1%8C%E4%B8%BA"><span class="nav-number">2.2.2.</span> <span class="nav-text">组织默认行为</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%8B%E4%BB%B6%E4%BB%A3%E7%90%86-%E4%BA%8B%E4%BB%B6%E5%A7%94%E6%89%98"><span class="nav-number">2.3.</span> <span class="nav-text">事件代理(事件委托)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8B%96%E6%8B%BD%E4%BA%8B%E4%BB%B6-drag"><span class="nav-number">2.4.</span> <span class="nav-text">拖拽事件 drag</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%A7%BB%E5%8A%A8%E7%AB%AF-touch"><span class="nav-number">2.5.</span> <span class="nav-text">移动端 touch</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%A7%BB%E5%8A%A8%E4%BA%8B%E4%BB%B6%E7%9A%84%E7%B1%BB%E5%9E%8B"><span class="nav-number">2.5.1.</span> <span class="nav-text">移动事件的类型</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#touch-%E4%BA%8B%E4%BB%B6%E5%AF%B9%E8%B1%A1%E5%B1%9E%E6%80%A7"><span class="nav-number">2.5.2.</span> <span class="nav-text">touch 事件对象属性</span></a></li></ol></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">Toby Lin</p>
  <div class="site-description" itemprop="description">無限進步!</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/personalBlog/archives/">
        
          <span class="site-state-item-count">2</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/personalBlog/categories/">
        <span class="site-state-item-count">3</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/personalBlog/tags/">
        <span class="site-state-item-count">6</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>



      </div>
      

    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Toby Lin</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-area"></i>
    </span>
    <span title="站点总字数">7k</span>
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">12 分钟</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://muse.theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

        








      </div>
    </footer>
  </div>

  
  <script src="/personalBlog/lib/anime.min.js"></script>
  <script src="/personalBlog/lib/velocity/velocity.min.js"></script>
  <script src="/personalBlog/lib/velocity/velocity.ui.min.js"></script>

<script src="/personalBlog/js/utils.js"></script>

<script src="/personalBlog/js/motion.js"></script>


<script src="/personalBlog/js/schemes/muse.js"></script>


<script src="/personalBlog/js/next-boot.js"></script>




  















  

  

</body>
</html>
